package com.hxzy.yygh.hosp.service.impl;


import com.alibaba.fastjson.JSON;
import com.hxzy.yygh.cmn.client.DictFeignClient;
import com.hxzy.yygh.hosp.repository.HospitalRepository;
import com.hxzy.yygh.hosp.service.HospitalService;
import com.hxzy.yygh.model.hosp.Hospital;
import com.hxzy.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //把map集合转换为实体类对象
        String jsonString = JSON.toJSONString(paramMap);
        Hospital hospital = JSON.parseObject(jsonString, Hospital.class);

        //根据医院编号，查询MongoBD数据库
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //判断该数据在数据库中是否存在
        if (hospitalExist == null){
            //执行新增操作
                //封装运维字段
                hospital.setCreateTime(new Date());
                hospital.setUpdateTime(new Date());
                hospital.setIsDeleted(0);
                hospital.setStatus(0);
                //执行新增方法
                hospitalRepository.save(hospital);
        }else{
            //执行修改操作
                //封装运维字段
                hospital.setCreateTime(hospitalExist.getCreateTime());
                hospital.setUpdateTime(new Date());
                hospital.setIsDeleted(hospitalExist.getIsDeleted());
                hospital.setStatus(hospitalExist.getStatus());
                //执行修改方法
                hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectHospByPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1, limit);

        //创建条件匹配器---准备example对象的匹配器参数
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //对象属性拷贝---准备example对象的对象参数
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        //创建example对象，并封装参数
        Example<Hospital> example = Example.of(hospital, matcher);

        //调用HospitalRepository方法，在mongoDB中查询数据
        Page<Hospital> pageInfo = hospitalRepository.findAll(example, pageable);
        pageInfo.getContent().stream().forEach(item ->{
            this.setHospitalHosType(item);
        });
        return pageInfo;
    }


    @Override
    public Hospital showDetails(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        return this.setHospitalHosType(hospital);
    }

    //根据医院编号,获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    //根据医院名称关键字搜索医院列表
    @Override
    public List<Hospital> getByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    //根据医院编号，获取医院详情信息
    @Override
    public Map<String, Object> getHosDetails(String hoscode) {
        //根据医院编号获取医院对象
        Hospital hospital = this.getByHoscode(hoscode);
        //根据医院对象，获取医院详情信息
        Hospital hospital1 = this.setHospitalHosType(hospital);

        //封装医院信息
        Map<String, Object> result = new HashMap<>();
        //封装预约规则
        result.put("bookingRule", hospital1.getBookingRule());

        //不需要重复返回
        hospital1.setBookingRule(null);

        //封装医院详情
        result.put("hospital", hospital1);

        //返回map封装对象
        return result;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }


    private Hospital setHospitalHosType(Hospital hospital) {
        //获取医院等级
        String hosTypeName = dictFeignClient.getDictName("Hostype", hospital.getHostype());
        //获取省名称
        String provinceName = dictFeignClient.getDictName(hospital.getProvinceCode());
        //获取市名称
        String cityName = dictFeignClient.getDictName(hospital.getCityCode());
        //获取区名称
        String districtName = dictFeignClient.getDictName(hospital.getDistrictCode());
        //封装参数
        hospital.getParam().put("hosTypeName",hosTypeName);
        hospital.getParam().put("addressName",provinceName+cityName+districtName);
        return hospital;
    }
}
